<?php
use PMA\libraries\Partition;
use PMA\libraries\Template;
use PMA\libraries\Util;

$rownum = 0; $odd_row = true; ?>
<form method="post" action="tbl_structure.php" name="fieldsForm" id="fieldsForm" class="ajax<?= $HideStructureActions; ?>">
    <?= PMA_URL_getHiddenInputs($db, $table); ?>
    <input type="hidden" name="table_type" value=
    <?php if ($db_is_system_schema): ?>
        "information_schema" />
    <?php elseif ($tbl_is_view): ?>
        "view" />
    <?php else: ?>
        "table" />
    <?php endif; ?>
    <table id="tablestructure" class="data topmargin">
        <!-- table header -->
        <?= PMA\libraries\Template::get('table/structure/table_structure_header')->render(
            array(
                'db_is_system_schema' => $db_is_system_schema,
                'tbl_is_view' => $tbl_is_view
            )
        ); ?>
        <tbody>
        <!-- table body -->
        <?php foreach($fields as $row): ?>
            <?php
                $rownum++;
                $columns_list[] = $row['Field'];
                $field_charset = $row['Collation'];

                $extracted_columnspec = Util::extractColumnSpec($row['Type']);
                $attribute = $extracted_columnspec['attribute'];
                if (strpos($row['Extra'], 'on update CURRENT_TIMESTAMP') !== false) {
                    $attribute = 'on update CURRENT_TIMESTAMP';
                }

                if (! isset($row['Default'])) {
                    if ($row['Null'] == 'YES') {
                        $row['Default'] = '<i>NULL</i>';
                    }
                } else {
                    $row['Default'] = htmlspecialchars($row['Default']);
                }

                $field_name    = htmlspecialchars($row['Field']);
                $displayed_field_name = $field_name;
                $comments = '';  //For column comments
                // underline commented fields and display a hover-title (CSS only)

                if (isset($comments_map[$row['Field']])) {
                    $displayed_field_name = '<span class="commented_column" title="'
                        . htmlspecialchars($comments_map[$row['Field']]) . '">'
                        . $field_name . '</span>';
                    $comments = htmlspecialchars($comments_map[$row['Field']]);
                }

                if ($primary && $primary->hasColumn($field_name)) {
                    $displayed_field_name .= Util::getImage(
                        'b_primary.png', __('Primary')
                    );
                }
                if (in_array($field_name, $columns_with_index)) {
                    $displayed_field_name .= Util::getImage(
                        'bd_primary.png', __('Index')
                    );
                }
            ?>
        <tr class="<?= ($odd_row ? 'odd': 'even'); ?>">
            <?php $odd_row = !$odd_row; ?>
            <?= PMA\libraries\Template::get('table/structure/table_structure_row')->render(
                array(
                    'row' => $row,
                    'rownum' => $rownum,
                    'displayed_field_name' => $displayed_field_name,
                    'type_nowrap' => Util::getClassForType($extracted_columnspec['type']),
                    'extracted_columnspec' => $extracted_columnspec,
                    'attribute' => $attribute,
                    'tbl_is_view' => $tbl_is_view,
                    'db_is_system_schema' => $db_is_system_schema,
                    'url_query' => $url_query,
                    'field_encoded' => urlencode($row['Field']),
                    'titles' => $titles,
                    'table' => $table,
                    'tbl_storage_engine' => $tbl_storage_engine,
                    'field_charset' => $field_charset,
                    'comments' => $comments
                )
            ); ?>
            <?php if (! $tbl_is_view && ! $db_is_system_schema): ?>
                <?= Template::get('table/structure/actions_in_table_structure')->render(
                    array(
                        'extracted_columnspec' => $extracted_columnspec,
                        'type' => (!empty($extracted_columnspec['print_type'])) ? $extracted_columnspec['print_type'] : ' ',
                        'tbl_storage_engine' => $tbl_storage_engine,
                        'primary' => $primary,
                        'field_name' => $field_name,
                        'url_query' => $url_query,
                        'titles' => $titles,
                        'row' => $row,
                        'rownum' => $rownum,
                        'columns_with_unique_index' => $columns_with_unique_index,
                        'isInCentralColumns' => in_array($row['Field'], $central_list) ? true : false
                    )
                ); ?>
            <?php endif; ?>
        </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?= Template::get('table/structure/check_all_table_column')->render(
        array(
            'pmaThemeImage' => $GLOBALS['pmaThemeImage'],
            'text_dir' => $GLOBALS['text_dir'],
            'tbl_is_view' => $tbl_is_view,
            'db_is_system_schema' => $db_is_system_schema,
            'tbl_storage_engine' => $tbl_storage_engine
        )
    ); ?>
</form>
<hr class="print_ignore"/>
<?= Template::get('table/structure/move_columns_dialog')->render(); ?>
<!--Work on the table-->
<div id="structure-action-links">
    <?php if ($tbl_is_view && ! $db_is_system_schema): ?>
        <?= Util::linkOrButton(
            $edit_view_url,
            Util::getIcon('b_edit.png', __('Edit view'), true)
        ); ?>
    <?php endif; ?>
    <?= Template::get('table/structure/optional_action_links')->render(
        array(
            'url_query' => $url_query,
            'tbl_is_view' => $tbl_is_view,
            'db_is_system_schema' => $db_is_system_schema
        )
    ); ?>
</div>
<?php if (! $tbl_is_view && ! $db_is_system_schema): ?>
    <br />
    <?= Template::get('table/structure/add_column')->render(
        array('columns_list' => $columns_list)
    ); ?>
<?php endif; ?>

<!--Displays indexes-->
<?php if (! $tbl_is_view
    && ! $db_is_system_schema && 'ARCHIVE' !=  $tbl_storage_engine): ?>
    <?= PMA_getHtmlForDisplayIndexes(); ?>
<?php endif; ?>

<!--Display partition details-->
<?php if (Partition::havePartitioning()):
    $partition_names = Partition::getPartitionNames($db, $table);

    // detect partitioning
    if (! empty($partition_names) && ! is_null($partition_names[0])):

        $partitions = Partition::getPartitions($db, $table);
        $firstPartition = $partitions[0];
        $rangeOrList = $firstPartition->getMethod() == 'RANGE'
            || $firstPartition->getMethod() == 'LIST';
        $subParitions = $firstPartition->getSubPartitions();
        $hasSubPartitions = $firstPartition->hasSubPartitions();
        if ($hasSubPartitions) {
            $firstSubPartition = $subParitions[0];
        }

        $actionIcons = array(
            'ANALYZE' => Util::getIcon('b_search.png', __('Analyze')),
            'CHECK' => Util::getIcon('eye.png', __('Check')),
            'OPTIMIZE' => Util::getIcon('normalize.png', __('Optimize')),
            'REBUILD' => Util::getIcon('s_tbl.png', __('Rebuild')),
            'REPAIR' => Util::getIcon('b_tblops.png', __('Repair')),
            'TRUNCATE' => Util::getIcon('b_empty.png', __('Truncate')),
        );
        if ($rangeOrList) {
            $actionIcons['DROP'] = Util::getIcon('b_drop.png', __('Drop'));
        }

        echo Util::getDivForSliderEffect(
            'partitions', __('Partitions')
        );

        $tmp_partition_description = $firstPartition->getDescription();

        $removeSQL = "ALTER TABLE " . Util::backquote($table) . " REMOVE PARTITIONING";
        $removeUrl = 'sql.php' . $url_query . '&sql_query=' . urlencode($removeSQL);

        echo Template::get('table/structure/display_partitions')->render(
            array(
                'db' => $db,
                'table' => $table,
                'url_query'  => $url_query,
                'partitions' => $partitions,
                'partitionMethod' => $firstPartition->getMethod(),
                'partitionExpression' => $firstPartition->getExpression(),
                'hasDescription' => ! empty($tmp_partition_description),
                'hasSubPartitions' => $hasSubPartitions,
                'subPartitionMethod' => $hasSubPartitions ? $firstSubPartition->getMethod() : null,
                'subPartitionExpression' => $hasSubPartitions ? $firstSubPartition->getExpression() : null,
                'actionIcons' => $actionIcons,
                'rangeOrList' => $rangeOrList,
                'removeUrl' => $removeUrl,
            )
        );
    else:
        echo Template::get('table/structure/display_partitions')->render(
            array(
                'db' => $db,
                'table' => $table,
            )
        );
    endif;
    ?>
    <!-- For closing Slider effect div-->
    </div>
<?php endif; ?>

<!--Displays Space usage and row statistics-->
<?php if ($GLOBALS['cfg']['ShowStats']): ?>
    <?= $tablestats;  ?>
<?php endif; ?>
<div class="clearfloat"></div>
